package com.example.spring.jpa.ast.operation;


import com.example.spring.jpa.ast.AstNode;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;
import java.util.ServiceLoader;

/**
 * @author: orange
 * @create: 2023-07-22 23:36
 * @description: 对称的表达式，该表达式的右边还会继续解析。
 */
public abstract class AstSymmetryOperation implements AstOperation, AstGrammar {
    private AstNode astNode;
    private String sentence;
    private static final Map<String, Class<? extends AstSymmetryOperation>> mapClass = new HashMap<>();
    static {
        ServiceLoader<AstSymmetryOperation> load = ServiceLoader.load(AstSymmetryOperation.class);
        for (AstSymmetryOperation astSymmetryOperation : load) {
            mapClass.put(astSymmetryOperation.operation(), astSymmetryOperation.getClass());
        }
    }
    @Override
    public AstNode parse() throws Exception {
        astNode = astNode.parse();
        return this;
    }

    public void setAstNode(AstNode astNode) {
        this.astNode = astNode;
    }

    @Override
    public String sentence() {
        return this.sentence;
    }

    public void setSentence(String sentence) {
        this.sentence = sentence;
    }

    public static AstSymmetryOperation getInstance(String operation)
            throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        Class<? extends AstSymmetryOperation> aClass = mapClass.get(operation);
        return aClass.getDeclaredConstructor().newInstance();
    }

    @Override
    public AstNode getLeft() {
        return null;
    }

    @Override
    public AstNode getRight() {
        return this.astNode;
    }
}
